package cate.game.role.res.inscription;

import cate.common.table.d.GDFunc;
import cate.common.table.d.GDInscription;
import cate.common.table.item.inscription.row.InscriptionPageRow;
import cate.common.util.GameResult;
import cate.game.role.FuncOpenChecker;
import cate.game.role.Role;
import cate.game.role.bag.inscription.Inscription;
import cate.game.role.bag.inscription.InscriptionPage;
import cate.game.role.res.inscription.msg.InscriptionDataResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.beans.Transient;
import java.util.HashMap;
import java.util.Map;

@NoteClass(value = "铭文")
public class RoleInscription extends FuncOpenChecker {
    @NoteField("玩家铭文页")
    public Map<Integer, InscriptionPage> pageMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (pageMap == null) {
            pageMap = new HashMap<>();
        }
        pageMap.values().forEach(item -> item.initialize(role));
    }

    @Override
    public int getFuncId() {
        return GDFunc.INSCRIPTION;
    }

    @Override
    public void onAfterOpen() {
        initPage();
    }

    //铭文页改名
    @JsonIgnore
    public void setName(int id, String name) {
        if (StringUtils.isBlank(name)) {
            role.getGame().notice.message(role, "修改失败，请输入名称");
            return;
        }
        if (name.length() > GDInscription.NAME_MAX_LENGTH) {
            role.getGame().notice.message(role, "名称长度不可超过" + GDInscription.NAME_MAX_LENGTH + "个");
            return;
        }
        boolean legal = role.getGame().tools.sensitiveWords.legal(name);
        if (!legal) {
            role.getGame().notice.message(role, "名称不合法");
            return;
        }
        for (InscriptionPage p :
                pageMap.values()) {
            if (p.id != id && StringUtils.equals(name, p.name)) {
                role.getGame().notice.message(role, "铭文页名字重复");
                return;
            }
        }
        InscriptionPage page = pageMap.get(id);
        if (page == null) {
            role.getGame().notice.message(role, "铭文页不存在");
            return;
        }
        page.name = name;
        noticeUpdate();
    }


    public void initPage() {
        for (InscriptionPageRow row : role.getGame().table.inscription.page.list) {
            if (!pageMap.containsKey(row.id)) {
                InscriptionPage page = new InscriptionPage(role);
                page.id = row.id;
                page.name = row.name;
                pageMap.put(row.id, page);
            }
        }
        noticeUpdate();
    }

    @Override
    public void onEnter() {
        super.onEnter();
    }


    public void noticeUpdate() {
        role.sendNow(new InscriptionDataResp(this));
    }

    public boolean contains(Inscription inscription) {
        for (InscriptionPage page : pageMap.values()) {
            if (page.uidMap.containsValue(inscription.uid)) {
                return true;
            }
        }
        return false;
    }

    @JsonIgnore
    @Transient
    public InscriptionPage getPage(int pageId) {
        return pageMap.get(pageId);
    }

    public GameResult<InscriptionPage> openGrid(int pageId,byte pos) {
        InscriptionPage page = getPage(pageId);
        if (page == null) {
            return new GameResult<InscriptionPage>().fail("铭文页不存在");
        }
        return page.openGrid(pos);
    }
}
